<?

/********************************************
* notifications.php
*
* This page displays a form to insert or delete notifications.
*
********************************************/

//include files
include "../config.php";
include "../../lib/db.php";
include "../../lib/tbatv_lib.php";

$title = "Notification Removal";

$relpath = "../";
include "../header.php";

//retrieve any form data:
$address = sanatizeGet($_REQUEST["emailaddress"]);
$debug = sanatizeGet($_REQUEST['debug']);

function formError() {
	//I don't know why I have to do this again, but I do. Otherwise they don't echo out properly. Are they not global?
	
	//retrieve any form data:
	$address = sanatizeGet($_REQUEST["emailaddress"]);
	$debug = sanatizeGet($_REQUEST['debug']);

	include "notification_deleteform.php"; // Show the form again!
	include "../footer.php";
	exit(); // if the error checking has failed, we'll exit the script!
}

?>
<h1>Notification Removal</h1>
<?

if($_REQUEST["submit"] == "submit") {

	if((!$address)) {
		echo '<div class="formItemWarning">You did not submit the following required information! <br />';
		if(!$address){
			echo "E-mail address is a required field. Please enter it below.<br />";
		}
	   	echo '</div>';
		
		formError();
	}

	$count_filter = "SELECT address FROM sms_notifications WHERE address = '$address'";
	$count_query = mysql_query($count_filter);
	if (mysql_num_rows($count_query) < 1) {
		echo "There are no notifications for <strong>" . $address . "</strong>, please double check that you entered your information correctly.";
		
		formError();
	} else {
		mysql_query("DELETE FROM sms_notifications WHERE address = '$address'");
		
		$subject="TBA";
		$message="You have been unsubscribed from TBA notifications. You may resubscribe at thebluealliance.net/tbatv/notifications";
		mail($address, $subject, $message,
			        "From: The Blue Alliance<texter@thebluealliance.net>\nX-Mailer: PHP/" . phpversion());
	}

?>
<h2>Notification Removed!</h2>

<p>You have unsubscribed for the address <? echo $address ?>. A message confirming your removal has been sent to that address.</p>

<?
} else { //This ends the "Submit" if. Now we include the "Not submitted" stuff.

	include "notification_deleteform.php";
}
include "../footer.php";
?>